package com.system.controller;

import com.common.annotation.MyLog;
import com.system.entity.Role;
import com.system.service.RoleService;
import com.system.vo.request.AddRoleReqVO;
import com.system.vo.request.RolePageReqVO;
import com.system.vo.request.RoleUpdateReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import javax.validation.Valid;
import java.util.List;
import com.common.res.DataResult;


import javax.validation.Valid;
import java.util.List;

/**
* @author XueHeng
* @since 2020-09-17
*/
@RestController
@RequestMapping("api")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/roles")
    @RequiresPermissions("sys:role:list")
    @ApiOperation("角色列表")
    @MyLog(title = "角色管理",action = "获取角色列表")
    public DataResult<List<Role>> getAllRoles(@RequestBody @Valid RolePageReqVO vo){
        DataResult result =DataResult.success();
        result.setData(this.roleService.getRoles(vo));
        return result;
    }

    @GetMapping("/role/{id}")
    @RequiresPermissions("sys:role:list")
    @ApiOperation("单个角色信息")
    @MyLog(title = "角色管理",action = "获取单个角色信息")
    public DataResult<Role> getRole(@NotBlank(message = "{required}") @PathVariable String id){
        DataResult result =DataResult.success();
        result.setData(this.roleService.getRole(id));
        return result;
    }

    @PostMapping("/role")
    @ApiOperation("新增")
    @MyLog(title = "角色管理",action = "新增角色")
    @RequiresPermissions("sys:role:add")
    public DataResult add(@Valid @RequestBody AddRoleReqVO vo) {
        DataResult result =DataResult.success();
        this.roleService.add(vo);
        return result;
    }

    @PutMapping("/role")
    @ApiOperation("修改")
    @MyLog(title = "角色管理",action = "修改角色")
    @RequiresPermissions("sys:role:update")
    public DataResult update(@Valid @RequestBody RoleUpdateReqVO vo) {
        DataResult result =DataResult.success();
        this.roleService.update(vo);
        return result;
    }


    @DeleteMapping(value = "/role/{id}")
    @ApiOperation("删除")
    @MyLog(title = "角色管理",action = "删除角色")
    @RequiresPermissions("sys:role:delete")
    public DataResult remove(@NotBlank(message = "{required}") @PathVariable String id) {
        DataResult result =DataResult.success();
        this.roleService.delete(id);
        return result;
    }
}
